package com.yoouce.patchmng.service;

import com.yoouce.patchmng.bean.RoleInfo;
import com.yoouce.patchmng.bean.UserInfo;
import com.yoouce.patchmng.init.SystemContants;
import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.*;

/**
 * 用户service处理类
 * Created by administrator on 15-9-28.
 */
@Service("UserService")
public class UserService {

    private static final String USER_ROLE_XML = "user-rule.xml";

    /**
     * 日志常量
     */
    private static final Logger logger = Logger.getLogger(UserService.class);

    /**
     * 获取系统用户信息列表
     *
     * @return sss
     */
    public void initUserInfo() {

        logger.info("加载XML:path=" + SystemContants.getRealClassPath() + USER_ROLE_XML);

        List<UserInfo> userInfos = new ArrayList<UserInfo>();
        List<RoleInfo> roleInfos = new ArrayList<RoleInfo>();


        // 创建saxReader对象
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            // 通过read方法读取一个文件 转换成Document对象
            document = reader.read(new File(SystemContants.getRealClassPath() + USER_ROLE_XML));
        } catch (DocumentException e) {
            logger.error("XML读取失败" + e.getMessage());
        }
        //获取根节点元素对象
        Element node = document.getRootElement();


        // 获取roles节点中
        Element rolesElement = node.element("roles");
        // roles下面子节点迭代器
        Iterator<Element> it = rolesElement.elementIterator();


        // 遍历
        while (it.hasNext()) {
            RoleInfo roleInfo = new RoleInfo();

            // 获取某个子节点对象
            Element e = it.next();
            List<Attribute> list = e.attributes();
            for (Attribute attr : list) {
                //将rolename的值放入到roleInfo对象中
                if ("rolename".equals(attr.getName())) {
                    roleInfo.setRoleName(attr.getValue());
                }
            }

            // 对子节点进行遍历
            Iterator<Element> pathIterator = e.elementIterator();
            List<String> paths = new ArrayList<String>();
            while (pathIterator.hasNext())
            {
                Element path = pathIterator.next();
                paths.add(path.getText());
            }
            roleInfo.setRolePath(paths);
            roleInfos.add(roleInfo);
        }

        // 获取users节点中
         rolesElement = node.element("users");
        // roles下面子节点迭代器
         it = rolesElement.elementIterator();

        // 遍历
        while (it.hasNext()) {
            UserInfo userInfo = new UserInfo();

            // 获取某个子节点对象
            Element e = it.next();
            List<Attribute> list = e.attributes();
            List<String> roleStringList =new ArrayList<String>();
            for (Attribute attr : list) {
                //将rolename的值放入到roleInfo对象中
                if ("username".equals(attr.getName())) {
                    userInfo.setUserName(attr.getValue());
                }
                if ("password".equals(attr.getName())) {
                    userInfo.setUserPassword(attr.getValue());
                }
                if ("roles".equals(attr.getName())) {
                    String rolesString = attr.getValue();

                    if (!StringUtils.isEmpty(roleInfos))
                    {
                        String[] tmp = rolesString.split(",");
                        Collections.addAll(roleStringList, tmp);
                    }
                    userInfo.setRoles(roleStringList);
                }
            }
            userInfos.add(userInfo);
        }

        //设置用户信息
        Map<String,UserInfo> userInfoMaps = new HashMap<String,UserInfo>();
        for(UserInfo userinfo:userInfos)
        {
            userInfoMaps.put(userinfo.getUserName(),userinfo);
        }
        SystemContants.setUsersMap(userInfoMaps);

        //设置用户权限
        Map<String,RoleInfo> roleInfoMaps = new HashMap<String, RoleInfo>();
        for(RoleInfo roleInfo:roleInfos)
        {
            roleInfoMaps.put(roleInfo.getRoleName(),roleInfo);
        }
        SystemContants.setRolesMap(roleInfoMaps);
        logger.error("加载XML结束");
    }


    /**
     * 用户权限
     * @return
     */
    public Map<String,RoleInfo> getRoles()
    {

        return null;
    }


}

